(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var tabgroups = new Array(), activeIndex = 0;
	var _currentActiveTab = null, _currentActiveIndex = null;

	var ui = $.layout('partial/bottom');
	var tabGroup = ui.view;
	var buttons = [ui.btn_0, ui.btn_1, ui.btn_2, ui.btn_3, ui.btn_4];

	for (var i = 0; i < 5; i++) {
		var imagePath = String.format(L('tabgroup'), i.toString());
		var selectedImagePath = String.format(L('tabgroup_on'), i.toString());
		buttons[i].image = imagePath;
		buttons[i].activeImage = selectedImagePath;
		buttons[i].originalImage = imagePath;
	}
	ui.badge_1.backgroundGradient = ui.badge_3.backgroundGradient = {
		type : 'linear',
		startPoint : {
			x : '0%',
			y : '0%'
		},
		endPoint : {
			x : '0%',
			y : '100%'
		},
		colors : [{
			color : '#E7BE55',
			offset : 0.0
		}, {
			color : '#DBAF46',
			offset : 0.5
		}, {
			color : '#C89927',
			offset : 1.0
		}],
	};
	_.each(buttons, function(item) {
		$.tap(item, function() {
			tabGroup.update_badge();
			var index = item.index;
			if (index == _currentActiveIndex)
				return;
			if (index != 1)
				$.mask(true, true);
			if (index == 0) {
				$.win('my_desires').hideView();
				$.win('my_desires').nav.reset();
				$.win('my_desires').display();
			} else if (index == 1) {
				$.win('my_cart').display();
			} else if (index == 2) {
				$.win('purchase').display();
			} else if (index == 3) {
				$.win('notifications').nav.reset();
				$.win('notifications').display();
			} else if (index == 4) {
				$.win('my_account').nav.reset();
				$.win('my_account').display();
			}
			$.ui('tabgroup').setActiveTab(index);
			tabGroup.active(index);
		});
	});

	tabGroup.active = function(index) {
		if (index == -1) {
			_currentActiveIndex = -1;
			_.each(buttons, function(button) {
				button.image = button.originalImage;
			});
		}
		if (_currentActiveTab)
			_currentActiveTab.image = _currentActiveTab.originalImage;
		if (index > -1 && index < 5) {
			_currentActiveIndex = index;
			_currentActiveTab = buttons[index];
			_currentActiveTab.image = _currentActiveTab.activeImage;
		}
	};
	tabGroup.update_badge = function() {
		_.each(tabgroups, function(ui) {
			var count_shoppingbad = $.badgeShoppingBag();
			var count_notification = $.badgeNotification();
			ui.label_badge_1.text = ' ' + count_shoppingbad + ' ';
			ui.label_badge_3.text = ' ' + count_notification + ' ';
			ui.badge_1.visible = parseInt(count_shoppingbad) > 0 ? true : false;
			ui.badge_3.visible = parseInt(count_notification) > 0 ? true : false;
		});
	};
	$.click(ui.badge_1, function() {
		ui.btn_1.fireEvent('singletap');
	});
	$.click(ui.badge_3, function() {
		ui.btn_3.fireEvent('singletap');
	});

	tabGroup.active(activeIndex);
	tabGroup.update_badge();
	tabgroups.push(ui);

	module.exports = tabGroup;
})();
